//
//  VolunteerClaimViewController.m
//  MemberProject
//
//  Created by vincent luo on 15/7/17.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "VolunteerClaimViewController.h"
#import "SelfClaimViewController.h"
@interface VolunteerClaimViewController ()
@property (strong,nonatomic)UIActionSheet *actionSheet;
@property (strong,nonatomic)NSDateFormatter *formatter;
@end

@implementation VolunteerClaimViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.dateTextField.text = [UserInfoManager sharedManager].userClaimInput.dangerDate;
    self.dateTextField.inputView = self.datePicker;
    [self.datePicker addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];
     self.formatter= [[NSDateFormatter alloc]init];
    [self.formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [self.formatter setDateFormat:@"yyyy-MM-dd"];
    
    self.title = @"自助申请";
    
    //创建工具条
         UIToolbar *toolbar=[[UIToolbar alloc]init];
         //设置工具条的颜色
         //设置工具条的frame
    toolbar.frame=CGRectMake(0, 0, SCREEN_WIDTH, 44);
    
         //给工具条添加按钮
    UIBarButtonItem *item0=[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(click:) ];

    
    UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
    item1.tag = 12;
    
             UIBarButtonItem *item2=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
          toolbar.items = @[item0,item2 ,item1 ];
         //设置文本输入框键盘的辅助视图
         self.dateTextField.inputAccessoryView=toolbar;
    
    if (self.isChangeInfo) {
        UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 32)];
        [backBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 18)];
        //    [backBtn setTitle:@"返回" forState:0];
        [backBtn setImage:[UIImage imageNamed:@"back_button.png"] forState:0];
        [backBtn addTarget:self action:@selector(dismissBack) forControlEvents:64];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    }
    
    
    
//
}
-(void)dismissBack
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

-(void)changeValue:(UIDatePicker *)sender
{
    NSDate *date = self.datePicker.date;
    
    NSString *dateStr = [self.formatter stringFromDate:date];
    
    self.dateTextField.text = dateStr;
}

-(void)click:(UIBarButtonItem *)sender
{
    
    [self.dateTextField resignFirstResponder];
    if (sender.tag==12) {
        NSDate *date = self.datePicker.date;

        NSDateFormatter *formatter= [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
        NSString *dateStr = [formatter stringFromDate:date];

        
        if (![Utils checkClaimDate:dateStr isBetweenDate:[UserInfoManager sharedManager].userClaimInput.activityBeginDate andDate:[UserInfoManager sharedManager].userClaimInput.activityEndDate]) {
            [Utils showAlert:@"出险日期必须处于活动期间"];
            return;
        }
//
        
        self.dateTextField.text = dateStr;
        [UserInfoManager sharedManager].userClaimInput.dangerDate = dateStr;
        
        SelfClaimViewController *selfVC = [[SelfClaimViewController alloc]initWithNibName:@"SelfClaimViewController" bundle:nil];
        if (self.isChangeInfo) {
            [selfVC setIsChangeInfo:YES];
        }
        
        selfVC.dangerDate = dateStr;
        [self.navigationController pushViewController:selfVC animated:YES];
        
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
